﻿// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.



using IdentityServer4.Models;
using System;
using System.Collections.Generic;
using IdentityModel;

using static IdentityServer4.IdentityServerConstants;
using ZFY.Core;
using ZFY.UMC.API.Model;
using ZFY.UMC.API.Model.Model;

namespace ZFY.UMC.Identity
{
    public static class Config
    {
        public static IEnumerable<IdentityResource> IdentityResources =>
            new IdentityResource[]
            {
                new IdentityResources.OpenId(),
                new IdentityResource("profile","User profile",ZFYProfileClaimTypes.UserProfile)
            };
        public static IEnumerable<IdentityServer4.Models.ApiResource> ApiResource =>
            new IdentityServer4.Models.ApiResource[]
            {
                new IdentityServer4.Models.ApiResource{ Scopes=new List<string>{ "PesonalWebsite.Api" }, Name="PesonalWebsite.Api", DisplayName="个人网站Api", UserClaims=new List<string>{ "role"}},
                new IdentityServer4.Models.ApiResource("identityApi","身份Api"){ Scopes=new List<string>{ "IdentityApi" }, UserClaims=new List<string>{ "role"}},
            };
        public static IEnumerable<ApiScope> ApiScopes = new List<ApiScope>
        {
            new ApiScope{ Name = "identityApi",DisplayName="身份Api",ShowInDiscoveryDocument =true }
        };
        public static IEnumerable<Client> Clients =>
            new Client[]
            { new Client{
                ClientName="ZFY.UMC.Vue",
                ClientId="ZFY.UMC.Vue",
                ClientUri="http://localhost:8080",
                RedirectUris=new List<string>
                {
                    "http://localhost:8080",
                    "http://localhost:8080/#/main",
                    "http://localhost:8080/#/login",
                    "http://localhost:8080/#/callback",
                    "http://localhost:8080/callback.html"
                },
                AllowedScopes=new HashSet<string>{ "openid","profile" },
                AllowedGrantTypes = GrantTypes.Code ,
                ClientSecrets = new List<Secret>{
                    new Secret("ZFY.UMC.Vue".Sha256(), "ZFY.UMC.Vue")
                },
                RequireClientSecret = false,
                AllowOfflineAccess = true,
                RefreshTokenUsage = TokenUsage.ReUse,
                AllowedCorsOrigins = new List<string>(){ "http://localhost:8080" },
                PostLogoutRedirectUris= new List<string>{ "http://localhost:8080/#/main" }
            },

               new IdentityServer4.Models.Client{
                   ClientName="ZFY.UMC.BlazorWebassembly",
                   RedirectUris=new List<string>{"http://localhost:5000/authentication/login-callback" },
                   ClientId="ZFY.UMC.BlazorWebassembly",

                   ClientUri="http://localhost:5000",
                   AllowedScopes=new HashSet<string>{ "identityApi","openid","profile",StandardScopes.OfflineAccess },
                   AllowedGrantTypes = GrantTypes.Code,
                   ClientSecrets = new List<Secret>
                   {
                       new Secret("PesonalWebsite.Vue".Sha256(), "ZFY.UMC.BlazorWebassembly")
                   },
                   AllowedCorsOrigins = new List<string>(){ "http://localhost:5000" },
                   RequireClientSecret = false,
                   AllowOfflineAccess = true,
                   RefreshTokenUsage = TokenUsage.ReUse,
                   RefreshTokenExpiration = TokenExpiration.Sliding,
                   AbsoluteRefreshTokenLifetime = 0,
                   SlidingRefreshTokenLifetime = (int)TimeSpan.FromDays(7).TotalSeconds,
                   UpdateAccessTokenClaimsOnRefresh = true,
                   PostLogoutRedirectUris = { "http://localhost:5000/authentication/logout-callback" },

                  RequirePkce = true,
               }
            };
        public static Role Role = new Role { Id = Guid.NewGuid(), Name = "admin" };
        public static Guid UserId = Guid.NewGuid();
        public static User User = new User
        {
            NickName = "茫雾",
            UserName = "admin",
            PasswordHash = @"AQAAAAEAACcQAAAAEFaX8CMbgXN+UlY9Rv0YnnlYRimaHkt1BbuhCXY3/Q8rDehrHc//LuVd2S7Unx8ObA==",
            Email = "798513422@qq.com",
            Id = UserId,
            NormalizedUserName = "ADMIN",

            UserRoles = new List<UserRole>
            {
                new UserRole
                {
                    UserId = UserId,
                    RoleId = Role.Id
                }
            },
            Claims = new List<UserClaim>
            {
                new UserClaim
                {   UserId = UserId,
                    ClaimType = JwtClaimTypes.Role,
                    ClaimValue = "管理员"
                }
            }
        };

    }
}